<template>
  <div>
<!-- 面包屑 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
        <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
        <el-breadcrumb-item>商品管理</el-breadcrumb-item>
        <el-breadcrumb-item>商品列表</el-breadcrumb-item>
    </el-breadcrumb>

<!-- 卡片 -->
    <el-card>
        <!-- 搜索与添加区域 -->
        <el-row :gutter="20">
            <el-col :span="10">
                <el-input placeholder="请输入内容" clearable v-model="querygoods.query" @clear='getgoodslist'>
                    <el-button slot="append" icon="el-icon-search" @click="getgoodslist"></el-button>
                </el-input>
            </el-col>
            <el-col :span="4">
                <el-button type="primary" @click="goaddgoods">添加商品</el-button>
            </el-col>
        </el-row>

        <!-- tab表格区域 -->
        <el-table
        :data="goodslist"
        style="width: 100%"
        stripe
        border>
        <!-- 索引列 -->
        <el-table-column type="index"></el-table-column>
        <!-- 商品列表区域 -->
        <el-table-column prop="goods_name" label="商品名称"></el-table-column>
        <el-table-column prop="goods_price" label="商品价格(元)" width="100px"></el-table-column>
        <el-table-column prop="goods_weight" label="商品重量" width="70px"></el-table-column>
        <el-table-column prop="add_time" label="创建时间" width="150px">
          <template slot-scope="scope">
            {{scope.row.add_time | dataformat}}
          </template>
        </el-table-column>
        <el-table-column label="操作" width="150px">
          <template slot-scope="scope">
            <el-button size="mini" type="primary" icon="el-icon-edit"></el-button>
            <el-button size="mini" type="danger" icon="el-icon-delete" @click="removebyid(scope.row.goods_id)"></el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页区域 -->
      <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="querygoods.pagenum"
      :page-sizes="[5, 10, 15, 20]"
      :page-size="querygoods.pagesize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total" background>
      </el-pagination>
    </el-card>
  </div>
</template>

<script>
export default {
  name: 'list',
  data(){
      return{
        //   商品列表
          goodslist:[],
        //   总共商品条数
          total:0,
        //   查询参数
        querygoods:{
            query:'',
            // 当前显示哪页
            pagenum:1,
            // 每页显示多少条
            pagesize:10,
        }
      }
  },
  methods:{
    //   获取所有商品列表
    async getgoodslist(){
        const {data:res}=await this.$axios.get('/goods',{params:this.querygoods})
        if(res.meta.status!==200) return this.$message.error('获取商品列表失败')
        this.total=res.data.total
        this.goodslist=res.data.goods
        console.log(res.data.goods)
    },
    // 监控每页的数量发生改变时触发,页码的大小，pagesize
    handleSizeChange(newsize){
      this.querygoods.pagesize=newsize
      this.getgoodslist()
    },
    // 监控当前页发生变化时触发
    handleCurrentChange(newpagenum){
      this.querygoods.pagenum=newpagenum
      this.getgoodslist()
    },
    // 点击删除商品按钮
    removebyid(id){
      // 弹出提示框，提示是否要删除操作
       this.$confirm('此操作将永久删除该商品, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.$axios.delete('/goods/'+id).then(res=>{
            if(res.data.meta.status!==200) return this.$message.error('删除商品失败')
            this.$message({
              type: 'success',
              message: '删除成功!'
            });
            this.getgoodslist()
          })

        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          });          
        });
    },
    // 点击添加商品按钮，跳转到add页面
    goaddgoods(){
      this.$router.push('/goods/add')
    }
  },
  created(){
      this.getgoodslist()
  }
}
</script>

<style lang="less" scoped>
</style>
